//
//  HMSportSportingViewController.m
//  XRSport
//
//  Created by acer on 16/10/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMSportSportingViewController.h"
#import "HMSportMapViewController.h"
#import "HMAddtional.h"
#import "HMSportSpeaker.h"
#import "HMSportCameraViewController.h"
@interface HMSportSportingViewController ()<HMSportMapViewControllerDelegate>
//运动的总距离
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
//运动的总时长
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;
//运动的平均速度
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLabel;
//右上角的地图按钮
@property (weak, nonatomic) IBOutlet UIButton *mapButton;
//暂停按钮
@property (weak, nonatomic) IBOutlet UIButton *pauseButton;
//继续按钮
@property (weak, nonatomic) IBOutlet UIButton *continueButton;

//开始停止湖暂停的x的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *endBtnCenterXConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueBtnCenterXConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pauseBtnCenterXConstraint;
//地图控制器
@property (nonatomic, strong) HMSportMapViewController *mapViewController;
@end

@implementation HMSportSportingViewController
{
    HMSportSpeaker *_sportSpeaker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _sportSpeaker = [HMSportSpeaker new];
    [self setupMapViewController];
    [self setupBackgroundLayer];
}
-(void)setupBackgroundLayer
{
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.bounds = self.view.bounds;
    layer.position = self.view.center;
    // 1> 设置渐变颜色数组
    CGColorRef color1 = [UIColor hm_colorWithHex:0x0e1428 alpha:1].CGColor;
    CGColorRef color2 = [UIColor hm_colorWithHex:0x406479 alpha:1].CGColor;
    CGColorRef color3 = [UIColor hm_colorWithHex:0x406578 alpha:1].CGColor;

    layer.colors = @[(__bridge UIColor *)color1,(__bridge UIColor *)color2,(__bridge UIColor *)color3];
    layer.locations = @[@0,@0.6,@1];
    [self.view.layer insertSublayer:layer atIndex:0];
}
//相机按钮背点击
- (IBAction)cameraBtnClick:(UIButton *)sender {
    
    HMSportCameraViewController *cameraVC = [HMSportCameraViewController new];
    
    [self presentViewController:cameraVC animated:YES completion:nil];
}
//地图按钮的点击事件
- (IBAction)mapViewBtnClick:(UIButton *)sender {
    
    
    [self presentViewController:_mapViewController animated:YES completion:nil];
}
//暂停继续停止按钮的点击事件
- (IBAction)sportStateBtnClick:(UIButton *)sender {
    
    HMSportState sportState = sender.tag;
    _mapViewController.sportTracking.sportState = sportState;
    CGFloat offset = (sportState == HMSportStatePause) ? -80 : 80;
    _endBtnCenterXConstraint.constant -= offset;
    _pauseBtnCenterXConstraint.constant += offset;
    _continueBtnCenterXConstraint.constant += offset;
    [UIView animateWithDuration:0.25 animations:^{
        _pauseButton.alpha = (sportState != HMSportStatePause);
        [self.view layoutIfNeeded];
    }];
    [_sportSpeaker sportState:sportState];
}
- (void)setupMapViewController
{
//    HMSportMapViewController *mapVC = [[HMSportMapViewController alloc] init];
//    [self addChildViewController:mapVC];
//    [self.view addSubview:mapVC.view];
//    mapVC.view.frame = self.view.bounds;
//    
//    //设置运动类型
//    mapVC.sportTracking = [[HMSportTracking alloc] initWithSportType:_sportType];
//    
//    [mapVC didMoveToParentViewController:self];
    // 1. 获取地图控制器
//    for (UIViewController *child in self.childViewControllers) {
//        if ([child isKindOfClass:[HMSportMapViewController class]]) {
//            _mapViewController = (HMSportMapViewController *)child;
//            
//            break;
//        }
//    }

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HMSportSporting" bundle:nil];
    _mapViewController = [sb instantiateViewControllerWithIdentifier:@"sportMapViewController"];
    _mapViewController.sportTracking = [[HMSportTracking alloc] initWithSportType:_sportType sportState:HMSportStateContinue];
    // 2. 设置运动轨迹模型
    _mapViewController.sportTracking = [[HMSportTracking alloc] initWithSportType:_sportType sportState:HMSportStateContinue];
    _mapViewController.delegate = self;
    
    [_sportSpeaker startSportWithType:_sportType];
}
//实现代理方法
-(void)sportMapDataChanged:(HMSportMapViewController *)vc
{
//    NSLog(@"%f",vc.sportTracking.totalDistance);
    HMSportTracking *sTracking = vc.sportTracking;
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f",sTracking.totalDistance];
    self.totalTimeLabel.text = sTracking.totalTimeStr;
    self.avgSpeedLabel.text = [NSString stringWithFormat:@"%.2f",sTracking.avgSpeed];
    
    if (sTracking.sportState == HMSportStatePause && _continueButton.alpha == 1) {
        [self sportStateBtnClick:_pauseButton];
    }
    if (sTracking.sportState == HMSportStateContinue && _continueButton.alpha == 0) {
        [self sportStateBtnClick:_continueButton];
    }
    [_sportSpeaker reportWithDistance:sTracking.totalDistance time:sTracking.totalTime speed:sTracking.avgSpeed];
}
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    //设置指南针的位置
    CGFloat x = _mapButton.center.x - _mapViewController.mapView.compassSize.width * 0.5;
    CGFloat y = _mapButton.center.y - _mapViewController.mapView.compassSize.height * 0.5;
    _mapViewController.mapView.compassOrigin = CGPointMake(x, y);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//修改状态栏文字的颜色
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


@end
